﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.Unity;

namespace Infrastructure.Web.Unity
{
    public class RequestLifetimeManager : LifetimeManager
    {
        public RequestLifetimeManager()
        {
        }
        public string Key { get; set; }
        public override object GetValue()
        {
            if (!string.IsNullOrEmpty(Key))
                return HttpContext.Current.Items[this.Key];
            else
                return null;
        }
        public override void RemoveValue()
        {
            if (HttpContext.Current.Items.Contains(Key))
                HttpContext.Current.Items.Remove(Key);
        }
        public override void SetValue(object newValue)
        {
            this.Key = newValue.GetType().FullName;
            HttpContext.Current.Items[this.Key] = newValue;
        }
    }
}
